﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using StudyEnglish.Model;

namespace StudyEnglish
{
    public partial class UserRegister : PhoneApplicationPage
    {
        BackgroundWorker worker;
        public UserRegister()
        {
            InitializeComponent();
        }

        private bool ValidConrol()
        {
            if (txt_username.Text.Length == 0)
            {
                MessageBox.Show("Hãy nhập Tên Đăng Nhập.", "Thông Báo", MessageBoxButton.OK);
                txt_username.Focus();
                return false;
            }
            else if (txt_password.Password.Length == 0)
            {
                MessageBox.Show("Hãy nhập Mật Khẩu", "Thông Báo", MessageBoxButton.OK);
                txt_password.Focus();
                return false;

            }
            else if (txt_password.Password != txt_ConfirmPassword.Password)
            {
                MessageBox.Show("Mật Khẩu và mật khẩu xác nhận không giống nhau", "Thông Báo", MessageBoxButton.OK);
                txt_ConfirmPassword.Focus();
                return false;
            }
            else
            {
                App.UserVM.getUser(txt_username.Text, txt_password.Password);
                if (App.UserVM.LstUser.Count() > 0)
                {
                    MessageBox.Show("Tên đăng nhập này đã tồn tại", "Thông Báo", MessageBoxButton.OK);
                    txt_username.Focus();
                    return false;
                }
            }
            return true;
        }

        private void InsertUser()
        {
            AppUser user = new AppUser
            {
                username = txt_username.Text,
                password = txt_password.Password,
                CountWord = (txt_num.Text==""?5:int.Parse(txt_num.Text))
            };
            App.UserVM.AddUser(user);
        }

        private void insertVolIntoDay()
        {
            int index = 0;
            int dayNum = 1;
            for (int i = 1; i < App.VolVM.LstVol.Count; i++)
            {
                if (index == App.Volnum)
                {
                    index = 0;
                    dayNum++;
                }
                AppDaily daily = new AppDaily
                {
                    UserID = App.UserVM.LstUser[0].ID,
                    Day = dayNum,
                    VocabularyID = App.VolVM.LstVol[i].ID
                    
                };
                index++;
                App.DayVM.AddDaily(daily);
                worker.ReportProgress((i * 100) / App.VolVM.LstVol.Count);
            }
        }

        private void RegisterProcess()
        {
            if (ValidConrol())
            {
                InsertUser();
                App.UserVM.getUser(txt_username.Text, txt_password.Password);
                if (App.UserVM.LstUser.Count() > 0)
                {
                    App.Volnum = App.UserVM.LstUser[0].CountWord;
                    App.userID = App.UserVM.LstUser[0].ID;
                    App.VolVM.GetLstVol();
                    if (App.chkInsert)
                    {
                        btn_start.IsEnabled = false;
                        worker = new BackgroundWorker();

                        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
                        worker.WorkerReportsProgress = true;
                        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                        worker.RunWorkerAsync();

                        App.chkInsert = false;
                    }
                }
                else
                {
                    MessageBox.Show("Đăng ký thất bại", "Thông Báo", MessageBoxButton.OK);
                }



            }
        }

        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            RegisterProcess();
            
        }
        

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

            insertVolIntoDay();

        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressPercentage.Value = e.ProgressPercentage;
            downloadPercentage.Text = "Tạo bài học mỗi ngày.... " + e.ProgressPercentage.ToString() + "%";
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/DailyList.xaml", UriKind.Relative));
        }

        private void txt_num_KeyUp(object sender, KeyEventArgs e)
        {
            var isEnterKey = e.Key == System.Windows.Input.Key.Enter || e.PlatformKeyCode == 10;

            if (isEnterKey)
            {
                RegisterProcess();
            }
        }

        private void txt_num_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 190)
            {
                e.Handled = true;
            }
        }
    }
}